package com.chenyue.cm.api.product;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.constant.ConstantValues;
import com.chenyue.cm.common.domain.JdAboutUs;
import com.chenyue.cm.common.domain.JdConstantValues;
import com.chenyue.cm.common.service.JdAboutUsService;
import com.chenyue.cm.common.service.JdConstantValuesService;
import com.chenyue.cm.common.vo.Page;
import com.chenyue.cm.order.domain.JdProduct;
import com.chenyue.cm.order.vo.ProductVo;
import com.chenyue.cm.payment.service.JdProductService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author libingxing
 */
@Api(tags = "商品模块（金币充值/月卡）")
@RestController
@RequestMapping("/api/product")
public class ApiProductController {

    private final JdProductService productService;

    private final JdAboutUsService aboutUsService;

    private final JdConstantValuesService constantValuesService;

    public ApiProductController(JdProductService productService, JdAboutUsService aboutUsService, JdConstantValuesService constantValuesService) {
        this.productService = productService;
        this.aboutUsService = aboutUsService;
        this.constantValuesService = constantValuesService;
    }

    /**
     * 获取商品列表
     *
     * @param type 商品类型
     */
    @GetMapping("/productList")
    @ApiOperation("商品列表")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页数", name = "page", dataType = "integer"),
            @ApiImplicitParam(value = "页码", name = "rows", dataType = "integer"),
            @ApiImplicitParam(value = "商品类型1金币2月卡", name = "type", dataType = "integer", required = true),
            @ApiImplicitParam(value = "机型 1安卓2ios", name = "deviceModel", dataType = "integer", required = true),
    })
    public Success<Page<List<JdProduct>>> productList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer rows,
            @RequestParam(defaultValue = "1") Integer type,
            Integer deviceModel
    ) {
        LambdaQueryWrapper<JdProduct> eq = new QueryWrapper<JdProduct>().lambda()
                .eq(JdProduct::getType, type)
                .eq(JdProduct::getDeviceModel, deviceModel);
        int count = productService.count(eq);
        Page<List<JdProduct>> result = new Page<>(page, rows, count);
        List<JdProduct> list = productService.list(eq.last(result.convertPage()));
        final JdConstantValues one = constantValuesService.lambdaQuery().eq(JdConstantValues::getConstantKey, ConstantValues.EXCHANGE_RATE).one();
        for (JdProduct product : list) {
            product.setExchangeRate(Integer.valueOf(one.getConstantValue()));
        }
        result.setContent(list);
        return Success.success(result);
    }

    @GetMapping("/productMonthCard")
    @ApiOperation("月卡商品")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "机型 1安卓2ios", name = "deviceModel", dataType = "integer", required = true),
    })
    public Success<ProductVo> productMonthCard(
            Integer deviceModel
    ) {
        JdProduct one = productService.getOne(new QueryWrapper<JdProduct>().lambda()
                .eq(JdProduct::getType, 2)
                .eq(JdProduct::getDeviceModel, deviceModel).last(" limit 1"));
        final ProductVo vo = new ProductVo();
        vo.setProduct(one);
//        会员权益
        vo.setContent(aboutUsService.lambdaQuery().eq(JdAboutUs::getId, 3).one().getContent());
        return Success.success(vo);
    }


}
